<?php 
  //////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2011 Aquevix Solutions Pvt Ltd. All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are
//  met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following disclaimer
//       in the documentation and/or other materials provided with the
//       distribution.
//     * Neither the name of Aquevix Solutions Pvt Ltd. nor the names of its
//       contributors may be used to endorse or promote products derived from
//       this software without specific prior written permission.
//     * Aquevix reserves the right to redistribute the source code, binaries,
//       derivative works, techniques, documents, designs and other techniques
//       used in this framework under a different license.
//
//     For any questions you may contact us at:
//
//       Attn:
//         Aquevix Solutions Pvt Ltd.
//         Suite 8-D, A-8 Bigjo's Tower,
//         Netaji Subhash Place,
//         New Delhi - 110034, INDIA
//
//       Contact:
//         http://www.aquevix.com
//         info@aquevix.com
//         +91-11-45600412
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
//  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//////////////////////////////////////////////////////////////////////////////
?>

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'UserController'.
 */
Yii::import('application.modules.user.components.UserIdentity');


class AutoLoginUserIdentity extends UserIdentity {
    
    public function __construct($username="", $password="") {
        parent::__construct($username, $password);
    }
    
    public function authenticate() {
        
        session_start();
        $autoId = $_SESSION["AUTOLOGIN_USER_ID"];
        $user = User::model()->findByPk($autoId);
        
        if ($user) {
            $this->id = $user->id;
            $this->setState('id', $user->id);
            $this->username = $user->username;
            $this->errorCode = self::ERROR_NONE;
        } else {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        }
        return !$this->errorCode;
    }

}


class AutoLoginUser extends UserLogin {
    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
//    public function rules() {
//        return array(
//            // username and password are required
//            array('username, password', 'required'),
//            // rememberMe needs to be a boolean
//            // password needs to be authenticated
//            array('password', 'authenticate'),
//        );
//    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute, $params) {


        if (!$this->hasErrors()) {  // we only want to authenticate when no input errors
            $identity = new AutoLoginUserIdentity();
            $identity->authenticate();

            switch ($identity->errorCode) {

                case UserIdentity::ERROR_EMAIL_INVALID:
                    $this->addError("username", Yii::t("UserModule.user", "Email is incorrect."));
                    break;
                case UserIdentity::ERROR_USERNAME_INVALID:
                    $this->addError("username", Yii::t("UserModule.user", "Username is incorrect."));
                    break;
                case UserIdentity::ERROR_STATUS_NOTACTIVE:
                    $this->addError("status", Yii::t("UserModule.user", "You account is not activated."));
                    break;
                case UserIdentity::ERROR_STATUS_BANNED:
                    $this->addError("status", Yii::t("UserModule.user", "You account is blocked."));
                    break;
                case UserIdentity::ERROR_PASSWORD_INVALID:
                    $this->addError("password", Yii::t("UserModule.user", "Password is incorrect."));
                    break;
                case UserIdentity::ERROR_NONE:
                    Yii::app()->user->id = $identity->id;
                    break;
            }
        }
    }

}



?>
